/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#define 	ADC_REFERENCE_MASK	0xC0
#define 	ADC_PRESCALE_MASK	0x07
#define 	ADC_MUX_MASK		0x1F
#define		ADC_10_BITS_MAX		1023.0f
#define		POWER_UP_ADC_CYCLES		25
#define		CONVERSION_ADC_CYCLES 	13

uint16_t ADCHardware::read()
{
	return *adc;
}

void ADCHardware::enableADC()
{
	setBit(*adcsra, ADEN);
}

void ADCHardware::disableADC()
{
	clearBit(*adcsra, ADEN);
}

void ADCHardware::enableAutoTriggering()
{
	setBit(*adcsra, ADATE);
}

void ADCHardware::disableAutoTriggering()
{
	clearBit(*adcsra, ADATE);
}

void ADCHardware::enableInterrupts()
{
	setBit(*adcsra, ADIE);
}

void ADCHardware::disableInterrupts()
{
	clearBit(*adcsra, ADIE);
}

bool ADCHardware::isConversionComplete()
{
	return bitIsSetInRegister(*adcsra, ADIF);
}

void ADCHardware::clearConversionCompleteFlag()
{
	setBit(*adcsra, ADIF);
}

void ADCHardware::setPrescaler(Prescaler prescaler)
{
	setConfig(adcsra, ADC_PRESCALE_MASK, prescaler);

}

void ADCHardware::setVoltageReference(VoltageReference voltageReference)
{
	setConfig(admux, ADC_REFERENCE_MASK, voltageReference);
}

void ADCHardware::setChannelMode(ChannelMode channelMode)
{
	setConfig(admux, ADC_MUX_MASK, channelMode);
}

void ADCHardware::leftAdjust()
{
	setBit(*admux, ADLAR);
}

void ADCHardware::rightAdjust()
{
	clearBit(*admux, ADLAR);
}

void ADCHardware::startConverter()
{
	setBit(*adcsra, ADSC); 					// start conversion
}

bool ADCHardware::isBusy()
{
	return bitIsSetInRegister(*adcsra, ADSC);
}



